<!--
 * @Description: 
 * @Author: zhuhao
 * @Date: 2020-11-12 11:26:24
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-04-13 16:45:34
-->
<template>
  <div class="statis_mian" v-loading='loading'>
      <div class="titlebar">
          <span class="back" @click="goBack"></span>
          <span class="title">每日填报</span>
          <span class='header_btn' v-if="isEdit&&isToday&&isVacation==0"  @click="addDailyIncomes">修改保存</span>
          <!-- <span class='header_btn' v-if="isEdit&&isToday&&isVacation==0"  @click="editDailyIncomes">修改保存</span> -->
          <span class='header_btn'  v-if='isToday&&!isEdit&&isVacation==0'  @click="addDailyIncomes">提交</span>
          <span class='header_btn'  v-if='isVacation==1' >休假中</span>
      </div>
        <!-- <div class="table_tit">
         {{userInfo.name+'-'+dateStr+'填报'}}
        </div>  -->
        <div class="header_select">
            <span style="color:#49c1ff">
                选择日期：
            </span>
            <el-date-picker
                size="small"
                v-model="statDate"
                type="date"
                :picker-options='pickerOptions'
                @change='dateChange'
                placeholder="选择日期">
            </el-date-picker>
        </div>
   
    <el-form ref="form" class="report_form" :rules="rules" :model="form" label-width="100px" size='mini'>
        <el-divider content-position="left"> <span class="table_tit">标准快递</span></el-divider>
        <el-form-item label="件数:" prop="standardMailCount">
            <el-input-number disabled  :precision="0" v-model="form.standardMailCount" class="input_left"  :min="0"></el-input-number>
            <!-- <el-input-number :disabled='form.isVacation==1||!isToday' :precision="0" v-model="form.standardMailCount" class="input_left"  :min="0"></el-input-number> -->
        </el-form-item>
        <el-form-item label="收入:" prop="standardMailIncome" >
            <el-input-number disabled :precision="2" v-model="form.standardMailIncome" class="input_left"  :min="0"></el-input-number>
            <!-- <el-input-number :disabled='form.isVacation==1||!isToday' :precision="2" v-model="form.standardMailIncome" class="input_left"  :min="0"></el-input-number> -->
        </el-form-item> 


        <!-- 展示字段 -->
        <el-form-item    label="当月完成件数:" prop="standardMailCompletedCount" >
        <!-- <el-form-item v-if="form.standardMailCompletedCount||form.standardMailCompletedCount==0"   label="当月完成件数:" prop="standardMailCompletedCount" > -->
            <el-input-number disabled  v-model="form.standardMailCompletedCount" class="input_left"   ></el-input-number>
        </el-form-item> 
        <el-form-item    label="当月完成收入:" prop="standardMailCompletedIncome" >
        <!-- <el-form-item v-if="form.standardMailCompletedIncome||form.standardMailCompletedIncome==0"   label="当月完成收入:" prop="standardMailCompletedIncome" > -->
            <el-input-number disabled :precision="2" v-model="form.standardMailCompletedIncome" class="input_left"  ></el-input-number>
        </el-form-item> 




        <el-divider content-position="left"> <span class="table_tit">散件计划</span></el-divider>
        <el-form-item label="散户件数:" prop="fragmentedMailCount">
            <el-input-number disabled  :precision="0"  v-model="form.fragmentedMailCount" class="input_left"  :min="0"></el-input-number>
            <!-- <el-input-number :disabled='form.isVacation==1||!isToday' :precision="0"  v-model="form.fragmentedMailCount" class="input_left"  :min="0"></el-input-number> -->
        </el-form-item>
        <el-form-item label="收入:" prop="fragmentedMailIncome">
            <el-input-number disabled  :precision="2"  v-model="form.fragmentedMailIncome" class="input_left"  :min="0"></el-input-number>
            <!-- <el-input-number :disabled='form.isVacation==1||!isToday' :precision="2"  v-model="form.fragmentedMailIncome" class="input_left"  :min="0"></el-input-number> -->
        </el-form-item>  

        <!-- 展示字段 -->
        <el-form-item    label="当月完成件数:" prop="fragmentedMailCompletedCount" >
        <!-- <el-form-item v-if="form.fragmentedMailCompletedCount||form.fragmentedMailCompletedCount==0"   label="当月完成件数:" prop="fragmentedMailCompletedCount" > -->
            <el-input-number disabled  v-model="form.fragmentedMailCompletedCount" class="input_left"   ></el-input-number>
        </el-form-item> 
        <el-form-item    label="当月完成收入:" prop="fragmentedMailCompletedIncome" >
        <!-- <el-form-item v-if="form.fragmentedMailCompletedIncome||form.fragmentedMailCompletedIncome==0"   label="当月完成收入:" prop="fragmentedMailCompletedIncome" > -->
            <el-input-number disabled :precision="2" v-model="form.fragmentedMailCompletedIncome" class="input_left"  ></el-input-number>
        </el-form-item> 




        <el-divider content-position="left"> <span class="table_tit">快递包裹</span></el-divider>
        <el-form-item label="件数:" prop="expressCount">
            <!-- <el-input-number disabled  :precision="0"  v-model="form.expressCount" class="input_left"  :min="0"></el-input-number> -->
            <el-input-number :disabled='form.isVacation==1||!isToday' :precision="0"  v-model="form.expressCount" class="input_left"  :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="收入:" prop="expressIncome">
            <!-- <el-input-number disabled :precision="2"  v-model="form.expressIncome" class="input_left"  :min="0"></el-input-number> -->
            <el-input-number :disabled='form.isVacation==1||!isToday' :precision="2"  v-model="form.expressIncome" class="input_left"  :min="0"></el-input-number>
        </el-form-item> 
        
        <!-- 展示字段 -->
        <el-form-item    label="当月完成件数:" prop="expressCompletedCount" >
        <!-- <el-form-item v-if="form.expressCompletedCount||form.expressCompletedCount==0"  label="当月完成件数:" prop="expressCompletedCount" > -->
            <el-input-number disabled  v-model="form.expressCompletedCount" class="input_left"   ></el-input-number>
        </el-form-item> 
        <el-form-item    label="当月完成收入:" prop="expressCompletedIncome" >
        <!-- <el-form-item v-if="form.expressCompletedIncome||form.expressCompletedIncome==0"  label="当月完成收入:" prop="expressCompletedIncome" > -->
            <el-input-number disabled :precision="2" v-model="form.expressCompletedIncome" class="input_left"  ></el-input-number>
        </el-form-item> 



        <el-divider content-position="left"> <span class="table_tit">走访客户数</span></el-divider>
        <el-form-item label="当日走访客户数:" prop="dailyVisit">
            <el-input-number  disabled :precision="0"  v-model="form.dailyVisit" class="input_left"  :min="0"></el-input-number>
            <!-- <el-input-number  :disabled='form.isVacation==1||!isToday' :precision="0"  v-model="form.dailyVisit" class="input_left"  :min="0"></el-input-number> -->
        </el-form-item> 
        <!-- 展示字段 -->
        <el-form-item    label="当月走访客户数:" prop="monthlyVisit" >
        <!-- <el-form-item v-if="form.monthlyVisit||form.monthlyVisit==0"  label="当月走访客户数:" prop="monthlyVisit" > -->
            <el-input-number disabled  v-model="form.monthlyVisit" class="input_left"   ></el-input-number>
        </el-form-item> 


        
        <el-divider content-position="left"> <span class="table_tit">投递数据</span></el-divider>
        <el-form-item label="当日投递总数:" prop="dailyDelivery">
            <el-input-number  disabled :precision="0"  v-model="form.dailyDelivery" class="input_left"  :min="0"></el-input-number>
            <!-- <el-input-number  :disabled='form.isVacation==1||!isToday' :precision="0"  v-model="form.dailyDelivery" class="input_left"  :min="0"></el-input-number> -->
        </el-form-item> 

        
        <!-- 展示字段 -->
        <el-form-item  label="当月投递总数:" prop="monthlyDelivery" >
        <!-- <el-form-item v-if="form.monthlyDelivery||form.monthlyDelivery==0" label="当月投递总数:" prop="monthlyDelivery" > -->
            <el-input-number disabled  v-model="form.monthlyDelivery" class="input_left"   ></el-input-number>
        </el-form-item> 
    </el-form>

    <!-- <div class="head_btn" v-if="isToday">

        <el-button size="mini" v-if="isEdit" type="primary" @click="editDailyIncomes">保存修改</el-button>
        <el-button size="mini" v-else type="primary" @click="addDailyIncomes">提交</el-button>
    </div> -->

  </div>
</template>

<script>
import API from "@/apis/jxyzwg.js";
import storage from 'good-storage'

export default {
    name:'UserDailyReports',
    data() {
        return { 
            userInfo:{},
            dateStr:'',
            form:{
                standardMailCount:0,
                standardMailIncome:0,
                fragmentedMailCount:0,
                fragmentedMailIncome:0,
                expressCount:0,
                expressIncome:0,
                dailyVisit:0,
                dailyDelivery:0,
            },
            isEdit:false,//是否属于修改
            loading:false,
            rules:{
                standardMailCount:[
                     { required: true, message: '请输入', trigger: 'blur' },
                ],
                standardMailIncome:[
                     { required: true, message: '请输入', trigger: 'blur' },
                ],
                fragmentedMailCount:[
                     { required: true, message: '请输入', trigger: 'blur' },
                ],
                fragmentedMailIncome:[
                     { required: true, message: '请输入', trigger: 'blur' },
                ],
                expressCount:[
                     { required: true, message: '请输入', trigger: 'blur' },
                ],
                expressIncome:[
                     { required: true, message: '请输入', trigger: 'blur' },
                ],
                dailyVisit:[
                     { required: true, message: '请输入', trigger: 'blur' },
                ],
                dailyDelivery:[
                     { required: true, message: '请输入', trigger: 'blur' },
                ]
            },
            statDate:new Date(new Date().getTime()-24*3600*1000),
            pickerOptions:{
                disabledDate(day){
                    return new Date(day).getTime()>new Date().getTime()-24*3600*1000
                }
            },
            isToday:true,
            isVacation:0
        }
    },
    created() {
        let today=new Date(new Date().getTime()-24*3600*1000)
        this.dateStr=today.getMonth()+1+'月'+today.getDate()+'日'
        let that=this  

        //测试用数据
        // this.userInfo.id="8f131513-affd-4308-b35c-17f2942f60da"
        // this.userInfo.name='谢姣'
        // this.getDailyIncomes() //发布前注释
        
       window.ado.parameters('token','tenant_code','user_id','user_account','user_name','tenant_name').then((res)=> {
                let data = res.data  
                this.userInfo = data;
                this.userInfo.id=this.userInfo.user_id
                this.userInfo.name=this.userInfo.user_name
                this.getDailyIncomes()
                }).catch(error => {
                console.log(error);
                this.$message.warning(error.message)
                });
    },
    methods: {
         
        addDailyIncomes(){
            this.$refs['form'].validate((valid) => {
                if (valid) {
                   let params=this.form
                    params.adoId=this.userInfo.id
                    // params.userName=this.userInfo.name
                    params.statDate=this.setDate(new Date().getTime()-24*3600*1000)
                    this.loading=true
                    params.isVacation=this.isVacation
                    
                    API.addDailyIncomes(params).then(res=>{
                            console.log(res);
                        if (res) {
                            this.loading=false
                            this.getDailyIncomes()
                            this.$message.success('提交成功')
                        } else {
                            this.$message.warning('提交失败')
                            this.loading=false
                        }
                    })
                } else {
                    this.$message.warning('请完整填写')
                    return false;
                }
            });
            
        },
        getDailyIncomes(){
            let params={
                // pageSize:1,
                // pageNum:1
            }
            params.adoId=this.userInfo.id
            // params.userName=this.userInfo.name
            params.statDate=this.setDate(this.statDate)
            this.loading=true
            this.form={}
            API.getDailyIncomes(params).then(res=>{ 
                if (res.code==200) {
                    if (res.rows&&res.rows[0]) {
                        this.form=res.rows[0]
                        this.isVacation=this.form.isVacation?this.form.isVacation:0
                        this.isEdit=true
                    }
                    this.loading=false
                    console.log(this.form);
                }else if (res.code==400){
                        this.$message.warning('该账号未录入系统')
                        this.loading=false

                        setTimeout(function(){
                            window.ado.close().then(res => {})

                        },2000)

                }else {
                         this.loading=false
                    this.$message.warning('查询历史数据失败')
                }
            })
        },
        editDailyIncomes(){
             this.$refs['form'].validate((valid) => {
                if (valid) {
                   let params=this.form
                    // params.adoId=this.userInfo.id
                    // params.userName=this.userInfo.name
                    params.statDate=this.setDate(new Date().getTime()-24*3600*1000)
                    this.loading=true
                    params.isVacation=this.isVacation
                    API.editDailyIncomes(params).then(res=>{
                        if (res) {
                            this.$message.success('提交成功')
                            this.getDailyIncomes()
                            this.loading=false

                        } else {
                            this.$message.warning('提交失败')
                            this.loading=false
                            
                        }
                    })
                } else {
                    this.$message.warning('请完整填写')
                    return false;
                }
            });
           
        },
        setDate(val){
            let date=new Date(val)
             let dayStr=date.getDate()<10?'0'+date.getDate():date.getDate()
             let monStr=date.getMonth()+1<10?'0'+(date.getMonth()+1):(date.getMonth()+1)
            return date.getFullYear()+'-'+monStr+'-'+dayStr
        },
        dateChange(val){
            console.log(this.statDate);
            if (this.setDate(this.statDate)!==this.setDate(new Date().getTime()-24*3600*1000)) {
                this.isToday=false
            } else {
                this.isToday=true
            }
            console.log(this.isToday);
            this.$refs['form'].resetFields();
            this.getDailyIncomes()
        },

         // 返回
      goBack(){
        window.ado.close().then(res => {})
      },
    },
}
</script>

<style lang="less" scoped>
.statis_mian{
    width: 100%;
    box-sizing: border-box;
    background: #fff;
    overflow-y: auto;
    // padding: 20px;
    display: flex;
    flex-direction: column;
    .head_btn{
        width: 100%;
        text-align: right;
        margin: 10px 0;
    }
    .table_tit{
        color: #49c1ff;
    }
    .input_left{
        width: 200px;
        display: block;
    }
    /deep/.el-form-item__label{
        font-size: 12px;
    }
    .titlebar{
        width:100%;
        height: 46px;
        line-height: 46px; 
        background: linear-gradient(to right, #177fff,#49c1ff);
        font-size: 18px;
        color: #ffffff;
        display: flex; 
        justify-content: center;
        }
    .titlebar .back{
        content: '';
        width: 46px;
        height: 46px;
        position: absolute;
         left: 0; 
        background: url("../../assets/images/icon-back.png") no-repeat center;
        background-size: 24px 24px;
     }
    .titlebar .back:focus{outline: none;}
    .header_btn{
        font-size: 14px;
         position: absolute;
         right: 10px; 
    }
    .report_form{
        box-sizing: border-box;
        padding: 0 20px;
    }

    .header_select{
        font-size: 14px;
        font-weight: 600;
        text-align: left;
        padding: 15px 20px;
        width: 100%;    
        box-sizing: border-box;
    }
} 
 
   
</style>